package com.eschool.beans;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.model.DualListModel;

import com.eschool.beans.converter.CourseConverter;
import com.eschool.beans.converter.SessionConverter;
import com.eschool.model.Clas;
import com.eschool.model.Course;
import com.eschool.model.Session;
import com.eschool.service.SchoolManagementService;
import com.eschool.service.impl.SchoolManagementServiceImpl;

@ManagedBean(name="classBean")
@ViewScoped
public class ClassBean {
	
	Clas clas = new Clas();
	
	List<com.eschool.model.Session> sessions;
	
	private DualListModel<Course> courses;

	SchoolManagementService service = new SchoolManagementServiceImpl();
	
	public ClassBean() {
		sessions = new ArrayList<com.eschool.model.Session>();
		sessions = SessionConverter.sessionDB;
		
		List<Course> target = new ArrayList<Course>();
		courses = new DualListModel<Course>(CourseConverter.courseDB, target);
	}
	
	public Clas getClas() {
		return clas;
	}

	public void setClas(Clas clas) {
		this.clas = clas;
	}
	
	public List<Session> getSessions() {
		return sessions;
	}

	public void setSessions(List<com.eschool.model.Session> sessions) {
		this.sessions = sessions;
	}
	
	public DualListModel<Course> getCourses() {
		return courses;
	}

	public void setCourses(DualListModel<Course> courses) {
		this.courses = courses;
	}

	public String performAction()
	{
		String message;
		
		//clas.setSession(SessionDao.getSessionByProperty("sessionId", 1));
		
		Set<Course> classCourses = new HashSet<Course>(courses.getTarget());
		clas.setCourses(new HashSet<Course>(courses.getTarget()));

		if(service.addClass(clas)) {
			message = "Class Successfully Added";
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Success Message", message));
		}
		else {
			message = "Class Not Added";
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Message", message));
		}
		
		return "AddClass";
	}



}
